package com.ruoyi.backend.controller;

import com.ruoyi.backend.domain.Product;
import com.ruoyi.backend.domain.vo.ProductVo;
import com.ruoyi.backend.service.IProductService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品详情Controller
 * 
 * @author wjr
 * @date 2025-09-23
 */
@RestController
@RequestMapping("/backend/t_product")
@Api(tags = "商品详情")
public class ProductController extends BaseController
{
    @Autowired
    private IProductService productService;

    /**
     * 查询商品详情列表
     */
    @PreAuthorize("@ss.hasPermi('backend:t_product:list')")
    @GetMapping("/list")
    @ApiOperation("查询商品详情列表")
    public TableDataInfo list(Product product)
    {
        startPage();
        List<ProductVo> list = productService.selectProductList(product);
        return getDataTable(list);
    }

    /**
     * 获取商品详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('backend:t_product:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取商品详情详细信息")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(productService.selectProductById(id));
    }

    /**
     * 新增商品详情
     */
    @PreAuthorize("@ss.hasPermi('backend:t_product:add')")
    @Log(title = "商品详情", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增商品详情")
    public AjaxResult add(@RequestBody Product product)
    {
        //获取当前登录的店铺id
        Integer shopId = SecurityUtils.getLoginUser().getUser().getShopId();
        if (shopId == null) {
            throw new ServiceException("无法获取店铺id");
        } else {
            product.setShopId(shopId);
        }
        return toAjax(productService.insertProduct(product));
    }

    /**
     * 修改商品详情
     */
    @PreAuthorize("@ss.hasPermi('backend:t_product:edit')")
    @Log(title = "商品详情", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改商品详情")
    public AjaxResult edit(@RequestBody Product product)
    {
        //获取当前登录的店铺id
        Integer shopId = SecurityUtils.getLoginUser().getUser().getShopId();
        if (shopId == null) {
            throw new ServiceException("无法获取店铺id");
        } else {
            product.setShopId(shopId);
        }

        return toAjax(productService.updateProduct(product));
    }

    /**
     * 删除商品详情
     */
    @PreAuthorize("@ss.hasPermi('backend:t_product:remove')")
    @Log(title = "商品详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除商品详情")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(productService.deleteProductByIds(ids));
    }

    /**
     * 修改产品是否可用状态
     */
    @PreAuthorize("@ss.hasPermi('backend:t_product:edit')")
    @Log(title = "商品详情", businessType = BusinessType.UPDATE)
    @PutMapping("/changeIsUsable")
    @ApiOperation("修改产品是否可用状态")
    public AjaxResult changeIsUsable(@RequestBody Product product)
    {
        return toAjax(productService.updateProduct(product));
    }

    /**
     * 导出商品详情列表
     */
    /*@PreAuthorize("@ss.hasPermi('backend:t_product:export')")
    @Log(title = "商品详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Product product)
    {
        List<Product> list = productService.selectProductList(product);
        ExcelUtil<Product> util = new ExcelUtil<Product>(Product.class);
        util.exportExcel(response, list, "商品详情数据");
    }*/

}
